"""
文档 https://fanyi-api.baidu.com/doc/22
"""
import hashlib
import random
import requests

import config

exec(open('../utils.py', 'r').read())

# url = 'http://api.fanyi.baidu.com/api/trans/vip/fieldtranslate'
url = 'https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate'


def sign(q: str, salt: int, domain: str) -> str:
    string_join = config.appid + q + str(salt) + domain + config.secretKey
    return hashlib.md5(string_join.encode()).hexdigest()


def get_params(q: str, from_lang: str = 'auto', to_lang: str = 'zh', domain: str = 'it') -> dict:
    salt = random.randint(32768, 65536)
    params = {
        'appid': config.appid,
        'q': q,
        'from': from_lang,
        'to': to_lang,
        'salt': str(salt),
        'domain': domain,
        'sign': sign(q, salt, domain)
    }
    return params


if __name__ == '__main__':
    text = get_text_from_clipboard()
    q = format_doxygen_comment(text)
    response = requests.get(
        url=url,
        params=get_params(q, 'en', 'zh', 'it')
    )
    for paragraph in response.json()['trans_result']:
        print(paragraph['src'])
    print('-' * 150)
    for paragraph in response.json()['trans_result']:
        print(paragraph['dst'])
